def min_operations(n, x, y):
    # 填充数字，使 x 和 y 长度相同
    x, y = str(x), str(y)
    x, y = x.zfill(n), y.zfill(n)

    carry = 0  # 进位/退位
    operations = 0

    for i in range(n - 1, -1, -1):
        # 计算当前位的差值，并加上前一位的进位/退位
        diff = (int(y[i]) - int(x[i]) + carry) % 10

        # 根据差值计算最小操作次数
        operations += min(diff, 10 - diff)

        # 更新进位/退位
        # 如果 y 的当前位减去 x 的当前位小于 0，则产生退位
        carry = -1 if int(y[i]) - int(x[i]) + carry < 0 else 0

    print(operations)

min_operations(5,12349,54321)
